#!/bin/sh
#
FETCH=1

if [ -f ./common_funcs.sh ]; then
    . ./common_funcs.sh
else
    echo "Cannot function without common_funcs.sh!" && exit 3
fi

function inst_qemu() {
    local qemu_ver="qemu-0.12.2"
    local qemu_url="http://download.savannah.gnu.org/releases/qemu/${qemu_ver}.tar.gz"

    if [ ${FETCH} -gt 0 ]; then
        if chk_cmd curl; then   # prefer curl for HTTP/1.1 support, multiple protocols, etc.
            curl -C - -O ${qemu_url}
        elif chk_cmd wget; then
            wget ${qemu_url}
        else
            echo "You must either have curl or wget to run this script!"
            exit 2
        fi

        bak_dir ${qemu_ver}

	    tar xvzf ${qemu_ver}.tar.gz # && rm ${qemu_ver}.tar.gz
    fi

    if [ ! ${FETCH} -eq 2 ]; then
	    (cd ${qemu_ver} && ./configure --target-list=i386-softmmu && make && sudo make install) || return 1
#	cd .. && sudo rm -d -R ${qemu_ver}
    fi
}

case ${1} in
    fetch)
        FETCH=2
        ;;
    nofetch)
        FETCH=0
        ;;
esac

inst_qemu || exit 1

#EOF
